﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Xml;
using System.IO.IsolatedStorage;
using System.IO;
using System.Windows.Resources;
using Facebook;
using Facebook.Reflection;
using System.Windows;

namespace Sai_Se_Dung
{
    public class Score
    {
        /// <summary>
        /// Read Score From File Score.txt
        /// </summary>
        /// <returns></returns>
        public int ReadScore()
        {
            #region Read from file .xml
            //XDocument xdoc = XDocument.Load(string.Format("Score.xml"));
            //var data = from query in xdoc.Element("Scores").Elements("Score")
            //           select new
            //           {
            //               highscore = (string)query.Element("HighScore")
            //           };

            //if (data.Count() > 0)
            //{
            //    return Int32.Parse(data.ElementAt(0).highscore);
            //}
            //else
            //{
            //    return 0;
            //}
            #endregion

            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var istream = new IsolatedStorageFileStream("Score.txt", FileMode.OpenOrCreate, FileAccess.Read, store))
                {
                    using (var sw = new StreamReader(istream))
                    {
                        String txt = sw.ReadLine();
                        if (!String.IsNullOrEmpty(txt))
                        {
                            return int.Parse(txt);
                        }
                        else
                        {
                            return 0;
                        }
                    }
                }
            }

        }

        /// <summary>
        /// Write Score To File Score.txt
        /// </summary>
        /// <param name="score"></param>
        public void WriteScore(string score)
        {
            using (var store = IsolatedStorageFile.GetUserStoreForApplication())
            {
                using (var istream = new IsolatedStorageFileStream("Score.txt", FileMode.OpenOrCreate,FileAccess.Write, store))
                {
                    using (var sw = new StreamWriter(istream))
                    {
                        sw.WriteLine(score);
                        sw.Close();
                    }
                }
            }
  
        }
    }
}

